package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chong Wang
 * @create 2020-11-30 13:45
 */
@Api(tags = "商品基本属性接口")
@RestController
@RequestMapping("/admin/product/")
@CrossOrigin
public class BaseManageController {

    @Autowired
    private ManageService manageService;

    @ApiOperation("获取所有一级分类")
    @GetMapping("getCategory1")
    public Result getCategory1(){
        return Result.ok(manageService.getCategory1());
    }

    @ApiOperation("根据一级分类id获取二级分类")
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id){
        return Result.ok(manageService.getCategory2(category1Id));
    }

    @ApiOperation("根据二级分类id获取三级分类")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id){
        return Result.ok(manageService.getCategory3(category2Id));
    }

    @ApiOperation("根据分类id获取平台属性信息")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable("category1Id") Long category1Id,
                               @PathVariable("category2Id") Long category2Id,
                               @PathVariable("category3Id") Long category3Id){
        return Result.ok(manageService.getAttrInfoList(category1Id, category2Id, category3Id));
    }

    //http://api.gmall.com/admin/product/saveAttrInfo
    @ApiOperation("添加或修改平台属性")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @ApiOperation("根据平台属性ID获取平台属性对象数据")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //先获取平台属性
        BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);
        //再根据平台属性获取平台属性值
        List<BaseAttrValue> list = baseAttrInfo.getAttrValueList();
        return Result.ok(list);
    }






}
